import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class SSCanvas extends Canvas implements Runnable
{
	
	private int x= 0;
	private int y= 0;
	
	private long timeIni;
	private int timeTotal;
	
	public void run()
	{
		
		initialize();
		
		
		while(true)
		{
				
			timeIni= System.currentTimeMillis(); 
			
			
			// Actualizar pantalla *******************
            repaint();
            serviceRepaints();
            //****************************************
            
            
           
            long timeEnd= System.currentTimeMillis();
            
            timeTotal= (int ) (timeEnd - timeIni);
            
            
            if(timeTotal < Define.frameRate)
            {
	            try
	            {
	            	Thread.sleep(Define.frameRate - timeTotal);
	            }
	            catch (InterruptedException e) 
	            {
	                System.out.println(e.toString());
	            }
	            
            }

		}
		
	}
	
	
	//******************* INITIALIZE *******************************
	public void initialize()
	{
		System.out.println("Inicializando Game...");
		
	}
	
	
	//********************* KEYS!!! ********************************
	public void keyPressed(int keyCode)
	{
		Control.readKeyPress(keyCode);
	}
	
	public void keyReleased(int keyCode)
	{
		Control.readKeyRelease(keyCode);
	}
	
	
	//******************* PAINT ***********************************
	public void paint(Graphics g)
	{
		g.setColor(0, 0, 0);
		g.fillRect(0, 0, getWidth(), getHeight());
		
		g.setColor(255, 0, 0);
		//g.drawString ("X: " + x, 0, 0, Graphics.LEFT | Graphics.TOP);
		g.drawRect(x, y, 2, 2);
		
		
		g.drawString("Keys: " + Define.KEY, 0, 0, Graphics.LEFT | Graphics.TOP);
	}
	
}	








/*	
	public void keyPressed(int keyCode) 
	{

		System.out.println("Tecla: " + keyCode);
		
		Control.readKeyPress(keyCode);
		
		
        int action=getGameAction(keyCode);

        switch (action) 
        {
            case LEFT:
            	Define.KEY |= Define.KEY_LEFT;
                x--;
                break;
            case RIGHT:
            	Define.KEY |= Define.KEY_RIGHT;
            	x++;
                break;
            case UP:
            	Define.KEY |= Define.KEY_UP;
            	y--;
                break;
            case DOWN:
            	Define.KEY |= Define.KEY_DOWN;
                y++;
            	break;
        }
        
        switch(keyCode)
        {
        	case -6:
        		Define.KEY |= Define.SOFT_KEY_LEFT;
        		break;
        	
        	case -7:
        		Define.KEY |= Define.SOFT_KEY_RIGHT;
        		break;
        
        	case 48:
        		Define.KEY |= Define.GK_0;
        		break;
        	
        	case 49:
        		Define.KEY |= Define.GK_1;
        		break;        	
 
        	case 50:
        		Define.KEY |= Define.GK_2;
        		break;        		
        
        	case 51:
        		Define.KEY |= Define.GK_3;
        		break;  
        		
        	case 52:
        		Define.KEY |= Define.GK_4;
        		break; 
        		
        	case 53:
        		Define.KEY |= Define.GK_5;
        		break; 
        		
        	case 54:
        		Define.KEY |= Define.GK_6;
        		break; 
        		
        	case 55:
        		Define.KEY |= Define.GK_7;
        		break; 
        		
        	case 56:
        		Define.KEY |= Define.GK_8;
        		break;
        		
        	case 57:
        		Define.KEY |= Define.GK_9;
        		break; 
        }
        
    } 
	*/
	

